package com.ls.fw.data.hbase.utils;


import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.Enumeration;

/**
 * 主机工具
 * <p>
 * </p>
 * 
 * @author ls 2013-9-14 下午02:18:34
 * @version V1.0
 * @modificationHistory=========================逻辑或功能性重大变更记录
 * @modify by user: {修改人} 2013-9-14
 * @modify by reason:{方法名}:{原因}
 */
public class HostUtil {

	/**
	 * 根据主机名称获取其IP
	 * 
	 * @author ls 2013-9-14 下午02:18:28
	 * @param hostname
	 * @return
	 */
	public static String getIPByHostname(String hostname) {
		String ip = hostname;
		try {
			InetAddress inet = InetAddress.getByName(hostname);
			ip = inet.getHostAddress();
		} catch (UnknownHostException e) {
			  e.printStackTrace();
		}
		return ip;
	}

	/**
	 * 根据IP获取主机名称
	 * 
	 * @author ls 2013-9-14 下午02:18:31
	 * @param ip
	 * @return
	 */
	public static String getHostnameByIP(String ip) {
		String hostname = ip;
		try {
			InetAddress inet = InetAddress.getByAddress(ip.getBytes());
			hostname = inet.getHostName();
		} catch (UnknownHostException e) {
			// e.printStackTrace();
		}
		return hostname;
	}

	public static String getLocalIP() {
		Enumeration<?> allNetInterfaces = null;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		InetAddress ip = null;
		String ipstr = "";
		if (allNetInterfaces != null) {
			label: while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
						.nextElement();
				// System.out.println(netInterface.getName());
				Enumeration<?> addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements()) {
					ip = (InetAddress) addresses.nextElement();
					if (ip != null && ip instanceof Inet4Address
							&& ip.isSiteLocalAddress()) {
						ipstr = ip.getHostAddress();
						int i = ipstr.lastIndexOf(".");
						String lastva = ipstr.substring(i+1);
						System.out.println(lastva);
						if(!lastva.equals("1")){
							break label;
						}
					}
				}
			}
		}

		return ipstr;
	}
    public static boolean isInnerIP(String ipAddress){   
        boolean isInnerIp = false;   
        long ipNum = getIpNum(ipAddress);   
        /**  
	        私有IP：A类  10.0.0.0-10.255.255.255  
	               B 类  172.16.0.0-172.31.255.255  
	               C 类  192.168.0.0-192.168.255.255  
	        当然，还有127这个网段是环回地址  
        **/  
        long aBegin = getIpNum("10.0.0.0");   
        long aEnd = getIpNum("10.255.255.255");   
        long bBegin = getIpNum("172.16.0.0");   
        long bEnd = getIpNum("172.31.255.255");   
        long cBegin = getIpNum("192.168.0.0");   
        long cEnd = getIpNum("192.168.255.255");   
        isInnerIp = isInner(ipNum,aBegin,aEnd) || isInner(ipNum,bBegin,bEnd) || isInner(ipNum,cBegin,cEnd) || ipAddress.equals("127.0.0.1");   
        return isInnerIp;              
}  
    
    private static long getIpNum(String ipAddress) {   
        String [] ip = ipAddress.split("\\.");   
        long a = Integer.parseInt(ip[0]);   
        long b = Integer.parseInt(ip[1]);   
        long c = Integer.parseInt(ip[2]);   
        long d = Integer.parseInt(ip[3]);   
      
        long ipNum = a * 256 * 256 * 256 + b * 256 * 256 + c * 256 + d;   
        return ipNum;   
    }  
    
    private static boolean isInner(long userIp,long begin,long end){   
        return (userIp>=begin) && (userIp<=end);   
   }  
	public static String screenLocalIP() {
		Enumeration<?> allNetInterfaces = null;
		try {
			allNetInterfaces = NetworkInterface.getNetworkInterfaces();
		} catch (SocketException e) {
			e.printStackTrace();
		}
		InetAddress ip = null;
		String ipstr = "";
		if (allNetInterfaces != null) {
		    while (allNetInterfaces.hasMoreElements()) {
				NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
						.nextElement();
				// System.out.println(netInterface.getName());
				Enumeration<?> addresses = netInterface.getInetAddresses();
				while (addresses.hasMoreElements()) {
					ip = (InetAddress) addresses.nextElement();
					System.out.println(ip.isMCSiteLocal());
				}
			}
		}

		return ipstr;
	}

	/**
	 * Get host IP address
	 * 
	 * @return IP Address
	 */
	public   static InetAddress getAddress() {
		try {
			for (Enumeration<NetworkInterface> interfaces = NetworkInterface
					.getNetworkInterfaces(); interfaces.hasMoreElements();) {
				NetworkInterface networkInterface = interfaces.nextElement();
				if (networkInterface.isLoopback()
						|| networkInterface.isVirtual()
						|| !networkInterface.isUp()) {
					continue;
				}
				Enumeration<InetAddress> addresses = networkInterface
						.getInetAddresses();
				if (addresses.hasMoreElements()) {
					return addresses.nextElement();
				}
			}
		} catch (SocketException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) throws UnknownHostException {
		System.out.println(isInnerIP("192.168.56.1"));
		try {
			InetAddress[] inetAdds = InetAddress.getAllByName(InetAddress
					.getLocalHost().getHostName());
			for (int i = 0; i < inetAdds.length; i++) {
				System.out.print(inetAdds[i].getHostName() + ":\t");
				System.out.println(inetAdds[i].getHostAddress());
			}

			Enumeration<?> allNetInterfaces = null;
			try {
				allNetInterfaces = NetworkInterface.getNetworkInterfaces();
			} catch (SocketException e) {
				e.printStackTrace();
			}
			InetAddress ip = null;
			String ipstr = "";
			if (allNetInterfaces != null) {
				while (allNetInterfaces.hasMoreElements()) {
					NetworkInterface netInterface = (NetworkInterface) allNetInterfaces
							.nextElement();
					System.out.println("***********");
				 System.out.println( netInterface.isLoopback()+":"+
						 netInterface.isVirtual()
							+":"+netInterface.isUp()); 
					Enumeration<?> addresses = netInterface.getInetAddresses();
					while (addresses.hasMoreElements()) {
						ip = (InetAddress) addresses.nextElement();
						ipstr = ip.getHostAddress();
						System.out.println(ipstr+":"+ip.isSiteLocalAddress());
					}
				}
			}
			System.out.println(getIPByHostname("localhost"));
		} catch (Exception e) {
			e.printStackTrace();
		}

	}
}
